package com.example.exercise.appmanage.user.service.impl;

import com.example.exercise.appmanage.user.mapper.UserMgMapper;
import com.example.exercise.appmanage.user.dto.response.UserResponse;
import com.example.exercise.appmanage.user.mapper.entity.UserEntity;
import com.example.exercise.appmanage.user.service.MgUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class MgUserServiceImpl implements MgUserService {

    @Resource
    private UserMgMapper userMgMapper;

    @Override
    public List<UserResponse> getUserList(String userName, Integer page, Integer pageSize) {
        List<UserResponse> userResponses = new ArrayList<>();
        Integer begin = (page - 1) * pageSize;
        List<UserEntity> userEntities = userMgMapper.getUserList(userName, begin, pageSize);
        for (UserEntity userEntity : userEntities) {
            UserResponse userResponse = new UserResponse();
            BeanUtils.copyProperties(userEntity,userResponse);
            userResponses.add(userResponse);
        }
        return userResponses;
    }

    @Transactional(readOnly = true)
    @Override
    public UserEntity findByUserId(Integer userId) {
        return userMgMapper.findByUserId(userId);
    }

    @Override
    public void modifyUseState(Integer userId) {
        userMgMapper.modifyUseState(userId);
    }

    @Transactional(readOnly = true)
    @Override
    public Integer getCount(String userName) {
        return userMgMapper.getTotalCount(userName);
    }

}
